from javax.swing import JFrame, JButton, JPanel, JTextField, JTextArea, JScrollPane, JLabel, JList, JComboBox
from javax.swing import BoxLayout, ListCellRenderer, DefaultListModel
from java.awt import BorderLayout, Color, GridLayout
from ConstellationWebFinder import findConstellationsWithZipCode,findExoplanetsWithZipCode,findNebulasWithZipCode,findConstellationsForNameInConstellationsTable,findExoplanetsForNameInExoplanetsTable, findNebulasForNameInNebulaTable


class guiFrame(object):
    def __init__(self):
        self.searchFor = JComboBox(["Everything","Constellation","Nebula","Exo-Planets"])
        self.searchBy = JComboBox(["ZIP Code","Name"])
        self.searchField = JTextField(30)
        searchButton = JButton("Search", actionPerformed = self.search)
        
        options = JPanel()
        options.add(JLabel("Search for: "))
        options.add(self.searchFor)
        options.add(JLabel("  Search by: "))
        options.add(self.searchBy)
        
        searchField = JPanel()
        searchField.add(self.searchField)
        searchField.add(searchButton)
        
        searchPanels = JPanel()
        searchPanels.setLayout(BoxLayout(searchPanels, BoxLayout.PAGE_AXIS))
        searchPanels.add(options)
        searchPanels.add(searchField)
        
        self.resultsModel = DefaultListModel()
        results = JList()
        results.model = self.resultsModel
        results.setCellRenderer(constellationEntry())
        
        self.mainPanel = JPanel()
        self.mainPanel.setLayout(BorderLayout())
        self.mainPanel.add(searchPanels, BorderLayout.NORTH)
        self.mainPanel.add(JScrollPane(results), BorderLayout.CENTER)
        
        self.frame = JFrame('Constellation Search')
        self.frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        self.frame.size = (700,700)
        self.frame.locationRelativeTo = None

        self.frame.add(self.mainPanel)
        self.frame.setVisible(True)
       
    def addResults(self,entries):
        for entry in entries:
            self.resultsModel.addElement(entry)
           
    def search(self,e):
        self.resultsModel.clear()
        searchVal = self.searchField.getText()
        loc = self.searchFor.getSelectedIndex()
        self.addResults([["<html><u>Name</u></html>","<html><u>Declination</u></html>","<html><u>Right Ascension</u></html>"]])
        if self.searchBy.getSelectedIndex() == 0:
            try:
                searchVal = int(searchVal)
                if loc == 1 or loc == 0:
                    self.addResults(findConstellationsWithZipCode(searchVal))
                if loc == 2 or loc == 0:
                    self.addResults(findNebulasWithZipCode(searchVal))
                if loc == 3 or loc == 0:
                    self.addResults(findExoplanetsWithZipCode(searchVal))
            except:
                pass
        else:
            if loc == 1 or loc == 0:
                temp = findConstellationsForNameInConstellationsTable(searchVal)
                if temp != None:
                    self.addResults(temp)
            if loc == 2 or loc == 0:
                temp = findExoplanetsForNameInExoplanetsTable(searchVal)
                if temp != None:
                    self.addResults(temp)
            if loc == 3 or loc == 0:
                temp = findNebulasForNameInNebulaTable(searchVal)
                if temp != None:
                    self.addResults(temp)
            print temp


class constellationEntry(JPanel,ListCellRenderer):
    def __init__(self):
        self.text = [JLabel() for i in range(4)]
        self.info = JLabel()
        
        infoBar = JPanel()
        infoBar.setLayout(GridLayout(1,4))
        for i in range(4):
            infoBar.add(self.text[i])

        self.setLayout(BorderLayout())
        self.add(infoBar,BorderLayout.CENTER)
    
    def initialize(self, list):
        i = 0
        while i < len(list):
            self.text[i].setText(list[i])
            i+=1
        while i < 4:
            self.text[i].setText('')
            i+=1
        
    def initializeDos(self, string, link):
        self.text[0].setText(r'<html>' + string + r'</html>')
        self.info[1].setText("URL: " + link)

    def getListCellRendererComponent(self,list,value,index,isSelected,cellHasFocus):
        if type(value)==type((0,0)):
            self.initializeDos(value[0],value[1])
        else:
            self.initialize(value)
        return self

def main():
    guiFrame()
if __name__ == '__main__':
    main()
